$('#table_list').DataTable({
    dom: '<"top">rt<"bottom"flip><"clear">',
    language: {
        "sUrl":'../static/plugins/datatables/language.json'
    },
    "searching": false,
    "processing": true,
    "serverSide": true,
    ajax:{
        url:'/shopping-mall/get-brand',
        type:'post',
        error:getError,
        data:function(d){
        	d.brand_name = $('#brand_name_search').val();
        },
    },
    aoColumns: [
        { "data": "", "bSortable": false},
        { "data": "id", "bSortable": true },
        { "data": "brand_name", "bSortable": false },
        { "data": "sort", "bSortable": true },
        { "data": "floor", "bSortable": false },
        { "data": "brand_category_name", "bSortable": false },
        { "data": "use_flg", "bSortable": false}
    ],
    columnDefs: [
    	{
            "targets": [0],
            "orderable":false,
            "searchable":false,
            "render": function(data, type, full) {
                return '<a href="javascript:" onclick="changeBrand('+ full.id +')">修改</a>';
            }
        },
        {
            "targets": [6],
            "orderable":false,
            "searchable":false,
            "render": function(data, type, full) {
                return '<span class="switch" data-id="'+ full.id +'" data-flg="'+(data == 1 ? 0 : 1)+'"><input  style="width:20px;height:20px;" type="checkbox" '+(data == 1 ? 'checked="checked"' : '')+' class="input-check"></span>';
            }
        },
    ],
    order: [[ 3, "asc"]]
});

function changeBrand(id){
	$('#brand-modal').modal('show');
	$.post('/shopping-mall/get-brand-detail',{id:id},success,'json');
    return false;
    function success(data){
        $.jBox.close();
        if(data.code == 200)
        {
            var res = data.val;
            $('#brand_name').val(res.brand_name);
            $('#sort').val(res.sort);
            //楼层
            var floor_num = res.floor_num;
            $('.mall-floor').html('<label for="tag_group_id_add">楼层</label>');
        	$('.brand-category').html('<label for="tag_group_id_add">品牌分类</label>');
        	var class_len = $('#class_len').val();
            var div_class = 'dropdown-sin-'+class_len;
        	var mall_floor_html = '<div class="'+div_class+'" style="width: 50%">';
        	mall_floor_html += '<select name="floor[]" id="floor" multiple placeholder="请选择">';
        	mall_floor_html += '<option value="-1" '+(floor_num.length == 0 ? 'selected' : '')+'>请选择</option>';
        	mall_floor.forEach(function(e,i){
        		mall_floor_html += '<option value="'+i+'" '+($.inArray(i.toString(),floor_num) >=0 ? 'selected' : '')+'>'+e+'</option>';
        	});
        	mall_floor_html += '</select>';
        	mall_floor_html += '</div>';
        	$('.mall-floor').append(mall_floor_html);
            $('.'+div_class).dropdown({
            	searchable: false
           	});
            class_len++;
            $('#class_len').val(class_len);
            //品牌分类
            var brand_category_id = res.brand_category_id;
            var class_len = $('#class_len').val();
            var div_class = 'dropdown-sin-'+class_len;
        	var brand_category_html = '<div class="'+div_class+'" style="width: 50%">';
        	brand_category_html += '<select name="brand_category[]" id="brand_category" multiple placeholder="请选择">';
        	brand_category_html += '<option value="-1" '+(brand_category_id.length == 0 ? 'selected' : '')+'>请选择</option>';
        	brand_category.forEach(function(e){
        		brand_category_html += '<option value="'+e.id+'" '+($.inArray(e.id.toString(),brand_category_id) >=0 ? 'selected' : '')+'>'+e.name+'</option>';
        	});
        	brand_category_html += '</select>';
        	brand_category_html += '</div>';
        	$('.brand-category').append(brand_category_html);
            $('.'+div_class).dropdown({
            	searchable: false
           	});
            class_len++;
            $('#class_len').val(class_len);
            if(is_null(res.brand_img) != null){
            	$('.brand_img').attr('src',imageBathUrl+res.brand_img);
                $('#brand_img').val(res.brand_img);
            }else{
            	$('.brand_img').attr('src','');
                $('#brand_img').val('');
            }
            if(is_null(res.brand_img_wap) != null){
            	$('.brand_img_wap').attr('src',imageBathUrl+res.brand_img_wap);
                $('#brand_img_wap').val(res.brand_img_wap);
            }else{
            	$('.brand_img_wap').attr('src','');
                $('#brand_img_wap').val('');
            }
            //mod by lizenan 20180212 【后台需求】商城导览/商城导览添加/修改界面去除品牌图片Alt和手机品牌图片Alt 515 start
            //$('#brand_img_alt').val(res.brand_img_alt);
            //$('#brand_img_wap_alt').val(res.brand_img_wap_alt);
            //mod by lizenan 20180212 【后台需求】商城导览/商城导览添加/修改界面去除品牌图片Alt和手机品牌图片Alt 515 end
            $('#country_name').val(res.country_name);
            if(is_null(res.country_img) != null){
            	$('.country_img').attr('src',imageBathUrl+res.country_img);
                $('#country_img').val(res.country_img);
            }else{
            	$('.country_img').attr('src','');
                $('#country_img').val('');
            }
            $('#use_flg').val(res.use_flg);
            $('#url').val(res.url);
            $('#remark').val(res.remark);
            $('#brand_id').val(res.id);
        }
        else{
            $.jBox.error(data.val, '错误');
        }
    }
}

function table_list_reload(){
    var table = $('#table_list').DataTable();
    table.ajax.reload();
}

$('table').on('click','.input-check',function(){
	var id = $(this).parent().attr('data-id');
	var use_flg = $(this).parent().attr('data-flg');
	changeBrandStatus(id,use_flg);
});

function changeBrandStatus(id,use_flg){
	title = '确定禁用此品牌吗？';
	if(use_flg == 1){
		title = '确定启用此品牌吗？';
	}
	$.jBox.confirm(title, "提示", function(v){
        if (v == 'ok'){
            $.post('/shopping-mall/change-brand-status',{id:id,use_flg:use_flg},success,'json');
            return false;
            function success(data){
                $.jBox.close();
                if(data.code == 200)
                {
                    $.jBox.tip('处理成功', 'success');
                    window.setTimeout(function () { table_list_reload(); }, 1000);
                }
                else{
                    $.jBox.error(data.val, '错误');
                }
            }
        }else{
        	if(use_flg == 1){
        		$('#table_list').find('span[data-id='+id+']').children('input').prop('checked',false);
        	}else{
        		$('#table_list').find('span[data-id='+id+']').children('input').prop('checked',true);
        	}
            $.jBox.close();
        }
    });
}

$('.add-brand').on('click',function(){
	$('#brand-modal').modal('show');
	$('#brand_name').val('');
	$('#sort').val('');
	$('.mall-floor').html('<label for="tag_group_id_add">楼层</label>');
	$('.brand-category').html('<label for="tag_group_id_add">品牌分类</label>');
	var class_len = $('#class_len').val();
    var div_class = 'dropdown-sin-'+class_len;
	var mall_floor_html = '<div class="'+div_class+'" style="width: 50%">';
	mall_floor_html += '<select name="floor[]" id="floor" multiple placeholder="请选择">';
	mall_floor_html += '<option value="-1" selected>请选择</option>';
	mall_floor.forEach(function(e,i){
		mall_floor_html += '<option value="'+i+'">'+e+'</option>';
	});
	mall_floor_html += '</select>';
	mall_floor_html += '</div>';
	$('.mall-floor').append(mall_floor_html);
    $('.'+div_class).dropdown({
    	searchable: false
   	});
    class_len++;
    $('#class_len').val(class_len);
    var class_len = $('#class_len').val();
    var div_class = 'dropdown-sin-'+class_len;
	var brand_category_html = '<div class="'+div_class+'" style="width: 50%">';
	brand_category_html += '<select name="brand_category[]" id="brand_category" multiple placeholder="请选择">';
	brand_category_html += '<option value="-1" selected>请选择</option>';
	brand_category.forEach(function(e){
		brand_category_html += '<option value="'+e.id+'">'+e.name+'</option>';
	});
	brand_category_html += '</select>';
	brand_category_html += '</div>';
	$('.brand-category').append(brand_category_html);
    $('.'+div_class).dropdown({
    	searchable: false
   	});
    class_len++;
    $('#class_len').val(class_len);
    $('.brand_img').attr('src','');
    $('#brand_img').val('');
    $('.brand_img_wap').attr('src','');
    $('#brand_img_wap').val('');
    //mod by lizenan 20180212 【后台需求】商城导览/商城导览添加/修改界面去除品牌图片Alt和手机品牌图片Alt 515 start
    //$('#brand_img_alt').val('');
    //$('#brand_img_wap_alt').val('');
    //mod by lizenan 20180212 【后台需求】商城导览/商城导览添加/修改界面去除品牌图片Alt和手机品牌图片Alt 515 end
    $('#country_name').val('');
    $('.country_img').attr('src','');
    $('#country_img').val('');
    $('#use_flg').val(1);
    $('#url').val('');
    $('#remark').val('');
    $('#brand_id').val('');
});

$('.ajax_post_tag').on('click',function(){
	var id = $('#brand-modal');
	var brand_name = $('#brand_name').val();
	if(brand_name == ''){
		$.jBox.error('请填写品牌名称', '错误');
		return false
	}
	var brand_img = $('#brand_img').val();
	if(brand_img == ''){
		$.jBox.error('请上传品牌图片', '错误');
		return false
	}
	var brand_img_wap = $('#brand_img_wap').val();
	if(brand_img_wap == ''){
		$.jBox.error('请上传手机品牌图片', '错误');
		return false
	}
	$.post($('#brand_form').attr('action'),$('#brand_form').serialize(),success,'json');
	return false;
	function success(data){
        if(data.code == 200)
        {
            $.jBox.tip('处理成功', 'success');
            id.modal('hide');
            window.setTimeout(function () { table_list_reload(); }, 1000);
        }
        else{
            $.jBox.error(data.val, '错误');
        }
    }
});

$('.upload-img-btn').click(function(){
	$('#img_class').val('upload-img-btn');
	$('.upload_file_ot').trigger('click');
});
$('.upload-img-btn-wap').click(function(){
	$('#img_class').val('upload-img-btn-wap');
	$('.upload_file_ot').trigger('click');
});
$('.upload-img-btn-country').click(function(){
	$('#img_class').val('upload-img-btn-country');
	$('.upload_file_ot').trigger('click');
});

$('.upload_file_ot').change(function(){
    if($(this).val() != ''){
    	$('#upload_form_update').submit();
    }
});

$('#upload_form_update').submit(function(e){
    e.preventDefault();
    $(this).ajaxSubmit({
        success:function(data){
            var result = $.parseJSON(data);
            if(result.code == 500){
                $.jBox.error(result.val, '错误');
            }else{
                var img_class = $('#img_class').val();
                if(img_class == 'upload-img-btn'){
                	$('.brand_img').attr('src',imageBathUrl+result.val);
                	$('#brand_img').val(result.val);
                }else if(img_class == 'upload-img-btn-wap'){
                	$('.brand_img_wap').attr('src',imageBathUrl+result.val);
                	$('#brand_img_wap').val(result.val);
                }else if(img_class == 'upload-img-btn-country'){
                	$('.country_img').attr('src',imageBathUrl+result.val);
                	$('#country_img').val(result.val);
                }
                $('#upload_form_update').find('input[type=file]').closest('form').get(0).reset();
            }
        }
    });

});
//add by lizenan 20180211 【后台需求】商城导览/商城导览增加搜索：品牌名称搜索 513 start
$('#reset_button').on('click',function(){
	$('#brand_name_search').val('');
	table_list_reload();
});
//add by lizenan 20180211 【后台需求】商城导览/商城导览增加搜索：品牌名称搜索 513 end